package cn.jboa.action;

import cn.jboa.common.Constants;
import cn.jboa.dao.CheckResultDao;
import cn.jboa.dao.ClaimVoucherDetailDao;
import cn.jboa.entity.BizClaimVoucher;
import cn.jboa.entity.BizClaimVoucherDetail;
import cn.jboa.entity.SysEmployee;
import cn.jboa.service.ClaimVoucherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ClaimVoucherAction extends BaseAction<BizClaimVoucher>{
    private BizClaimVoucher claimVoucher;

    private List<BizClaimVoucherDetail> detailList;

    //private ClaimVoucherService claimVoucherService;
    @Autowired
    private Map<String, ClaimVoucherService> claimVoucherServicesMapping;

    @Autowired
    @Qualifier("claimVoucherDetailDao")
    private ClaimVoucherDetailDao claimVoucherDetailDao;


    private String status;
    private Date startDate;
    private Date endDate;
   /* private static Map<String,String> statusMap;
    static {
        statusMap=new LinkedHashMap<>();
        statusMap.put(Constants.CLAIMVOUCHER_CREATED,"新创建");
        statusMap.put(Constants.CLAIMVOUCHER_SUBMITTED,"已提交");
        statusMap.put(Constants.CLAIMVOUCHER_APPROVING,"待审批");
        statusMap.put(Constants.CLAIMVOUCHER_BACK,"已打回");
        statusMap.put(Constants.CLAIMVOUCHER_APPROVED,"已审批");
        statusMap.put(Constants.CLAIMVOUCHER_PAID,"已付款");
        statusMap.put(Constants.CLAIMVOUCHER_TERMINATED,"已终止");
    }*/

    public String searchClaimVoucher(){
        switch (this.getLoginEmployee().getSysPosition().getNameCn()) {
            case Constants.POSITION_CASHIER:
                this.cashierStatusInit();
                break;
            case Constants.POSITION_FM:
                this.fmStatusInit();
                break;
            case Constants.POSITION_GM:
                this.gmStatusInit();
                break;
            default:
                this.stuffStatusInit();
                break;
        }
        pageSupport = getClaimVoucherService().getClaimVoucherPage(
                this.getLoginEmployee(), status, startDate, endDate, pageNo,
                pageSize);
/*		pageSupport = claimVoucherService.getClaimVoucherPage(this
				.getLoginEmployee().getSn(), status, startDate, endDate,
				pageNo, pageSize);
*/		return "list";
    }

    protected void stuffStatusInit() {
    }

    protected void fmStatusInit() {
    }

    protected void gmStatusInit() {
        if (status == null)
            status = Constants.CLAIMVOUCHER_APPROVING;
    }

    protected void cashierStatusInit() {
        if (status == null)
            status = Constants.CLAIMVOUCHER_APPROVED;
    }



    public String getClaimVoucherById(){
        claimVoucher=getClaimVoucherService().findClaimVoucherById(claimVoucher.getId());
        return "view";
    }

    public String toUpdate(){
        claimVoucher=getClaimVoucherService().findClaimVoucherById(claimVoucher.getId());
        return "update";
    }

    public String updateClaimVoucher(){
        if(claimVoucher.getStatus().equals(Constants.CLAIMVOUCHER_SUBMITTED)){
            claimVoucher.setNextDeal((SysEmployee) this.getSession().get(Constants.AUTH_EMPLOYEE_MANAGER));
        }
        claimVoucher.setDetailList(detailList);
        getClaimVoucherService().updateClaimVoucher(claimVoucher);
        return "redirectList";
    }



    public String saveClaimVoucher(){
        claimVoucher.setCreator(this.getLoginEmployee());
        if (claimVoucher.getStatus().equals(Constants.CLAIMVOUCHER_SUBMITTED)) {
            claimVoucher.setNextDeal((SysEmployee) this.getSession().get(
                    Constants.AUTH_EMPLOYEE_MANAGER));
        } else {
            claimVoucher.setNextDeal(claimVoucher.getCreator());
        }
        claimVoucher.setDetailList(detailList);
        for (BizClaimVoucherDetail d : detailList) {
            d.setBizClaimVoucher(claimVoucher);
        }
        getClaimVoucherService().saveClaimVoucher(claimVoucher);
        return "redirectList";
    }

    public String deleteClaimVoucherById(){
        getClaimVoucherService().deleteClaimVoucherById(claimVoucher);
        return "redirectList";
    }

    public String toCheck() {
        claimVoucher = getClaimVoucherService().findClaimVoucherById(claimVoucher
                .getId());
        return "check";
    }




    public List<BizClaimVoucherDetail> getDetailList() {
        return detailList;
    }

    public void setDetailList(List<BizClaimVoucherDetail> detailList) {
        this.detailList = detailList;
    }

    public BizClaimVoucher getClaimVoucher() {
        return claimVoucher;
    }

    public void setClaimVoucher(BizClaimVoucher claimVoucher) {
        this.claimVoucher = claimVoucher;
    }




    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    /*public Map<String, String> getStatusMap() {
        return statusMap;
    }*/

    public ClaimVoucherDetailDao getClaimVoucherDetailDao() {
        return claimVoucherDetailDao;
    }

    public void setClaimVoucherDetailDao(ClaimVoucherDetailDao claimVoucherDetailDao) {
        this.claimVoucherDetailDao = claimVoucherDetailDao;
    }

    public Map<String, String> getStatusMap() {
        return this.getClaimVoucherService().getStatusMap();
    }


    public Map<String, ClaimVoucherService> getClaimVoucherServicesMapping() {
        return claimVoucherServicesMapping;
    }

    public void setClaimVoucherServicesMapping(Map<String, ClaimVoucherService> claimVoucherServicesMapping) {
        this.claimVoucherServicesMapping = claimVoucherServicesMapping;
    }
    public ClaimVoucherService getClaimVoucherService() {
        return this.getClaimVoucherServicesMapping().get("claimVoucherService"+this.getPositionForShort());
    }
}
